'use strict';

const Controller = require('egg').Controller;

class BrokerController extends Controller {
    //获取
    async getbroker() {
        const { ctx } = this;
        const { state } = ctx.query;
        const data = await this.app.mysql.query(`SELECT * FROM broker WHERE state Like "%${state}%"`);
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //搜索
    async searchbroker() {
        const { ctx } = this;
        const { name, state } = ctx.query;
        const data = await this.app.mysql.query(`SELECT * FROM broker WHERE state Like "%${state}%" 
        and name Like "%${name}%"`);
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //删除
    async deletebroker() {
        const { ctx } = this;
        const { id } = ctx.query;
        const data = await this.app.mysql.delete('broker', {
            id: id,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "删除失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "删除成功"
        };
    }

    //新增
    async addbroker() {
        const { ctx } = this;
        const { arr } = ctx.request.body;
        const data = await this.app.mysql.insert('broker', {
            name: arr.name,
            key: 0,
            phone: arr.phone,
            company: arr.company,
            newhome: 0,
            twohome: 0,
            renthome: 0,
            state: "在职"
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "新增失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "新增成功"
        };
    }


    //编辑
    async editorbroker() {
        const { ctx } = this;
        const { arr } = ctx.request.body;
        const list = await this.app.mysql.get('broker', { id: arr.id });
        const data = await this.app.mysql.update('broker', {
            name: arr.name,
            key: list.key,
            phone: arr.phone,
            company: arr.company,
            newhome: list.newhome,
            twohome: list.twohome,
            renthome: list.renthome,
            state: "在职",
            id: list.id
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "修改失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "修改成功"
        };
    }

    //启用
    async statebroker() {
        const { ctx } = this;
        const { id, type } = ctx.request.body;
        let list = await this.app.mysql.get('broker', { id: id });
        list.state = type;
        const data = await this.app.mysql.update('broker', {
            ...list
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "修改失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "修改成功"
        };
    }
}

module.exports = BrokerController;
